package com.czy.owner.ui.message;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.czy.owner.R;
import com.czy.owner.adapter.ContactsLabelAdapter;
import com.czy.owner.entity.MessageContacts;
import com.czy.owner.global.Constants;
import com.czy.owner.ui.BaseActivity;
import com.czy.owner.utils.PhoneUtils;
import com.czy.owner.widget.DialogCall;
import com.czy.owner.widget.PageTitleView;
import com.czy.owner.widget.RoundImageView;
import com.hyphenate.easeui.EaseConstant;

import org.xutils.common.Callback;
import org.xutils.x;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import butterknife.BindView;

/**
 * Created by King on 2016/8/4 0004.
 */
public class ContactsPanelActivity extends BaseActivity {

    @BindView(R.id.iv_contacts_avatar)
    RoundImageView ivAvatar;
    @BindView(R.id.tv_contacts_name)
    TextView tvName;
    @BindView(R.id.iv_contacts_gender)
    ImageView ivGender;
    @BindView(R.id.tv_contacts_phone_number)
    TextView tvPhoneNumber;
    @BindView(R.id.iv_contacts_call)
    ImageView ivCall;
    @BindView(R.id.tv_contacts_group)
    TextView tvGroup;
    @BindView(R.id.tv_contacts_experience)
    TextView tvExperience;
    @BindView(R.id.rv_contacts_labels)
    RecyclerView rvLabels;
    @BindView(R.id.btn_contacts_message)
    Button btnMessage;

    private MessageContacts mContacts = null;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_contacts_panel;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initViews() {
        if (getIntent().getExtras() == null) {
            PhoneUtils.ShowToastMessage(this, "数据异常");
            finish();
            return;
        }

        PageTitleView pageTitle = new PageTitleView((RelativeLayout)findViewById(R.id.rl_page_title), "详细资料", true);
        pageTitle.getLeftButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mContacts = (MessageContacts) getIntent().getExtras().getSerializable(Constants.TRANSMISSION_CONTENT_KEY);
        if(!TextUtils.isEmpty(mContacts.getUserLogo())){
            x.image().bind(ivAvatar, mContacts.getUserLogo().trim(), new Callback.CommonCallback<Drawable>() {
                @Override
                public void onSuccess(Drawable result) {

                }

                @Override
                public void onError(Throwable ex, boolean isOnCallback) {
                    ivAvatar.setImageResource(R.mipmap.icon_mechanic_none);
                }

                @Override
                public void onCancelled(CancelledException cex) {

                }

                @Override
                public void onFinished() {

                }
            });
        }else{
            ivAvatar.setImageResource(R.mipmap.icon_connect_portrait);
        }

        tvName.setText(mContacts.getEmployeesName());
        if (mContacts.getSex() == 1) {
            ivGender.setImageResource(R.mipmap.icon_male);
        } else {
            ivGender.setImageResource(R.mipmap.icon_female);
        }
        tvPhoneNumber.setText(mContacts.getContact());
        Pattern p = Pattern.compile("[\\d|\\-]+");
        final Matcher m = p.matcher(mContacts.getContact());
        if (m.find()  && !TextUtils.isEmpty(m.group(0))) {
            ivCall.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    final DialogCall dialogCall = new DialogCall(ContactsPanelActivity.this, R.layout.dialog_call);
                    dialogCall.setContent("" + m.group(0)).setListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            PhoneUtils.checkCallPermission(ContactsPanelActivity.this, m.group(0));
                            dialogCall.dismiss();
                        }
                    }).show();
                }
            });
        } else {
            ivCall.setImageResource(R.mipmap.icon_call_gray);
        }

        tvGroup.setText(mContacts.getPositionName());
        int intTmp = (int) mContacts.getJobYear();
        double doubleTmp = mContacts.getJobYear() - intTmp;
        if (doubleTmp == 0) {
            tvExperience.setText(intTmp + "年");
        } else {
            tvExperience.setText(intTmp + "年" + (int) (doubleTmp * 12) + "月");
        }

        if(!TextUtils.isEmpty(mContacts.getLabels())){
            List<String> labelList = new ArrayList<>();
            Collections.addAll(labelList, mContacts.getLabels().split(","));

            ContactsLabelAdapter mAdapter = new ContactsLabelAdapter(ContactsPanelActivity.this,labelList);
            RecyclerView.LayoutManager layoutManager = new GridLayoutManager(this, 3);
            rvLabels.setLayoutManager(layoutManager);
            rvLabels.setAdapter(mAdapter);
        }
        btnMessage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
                Intent intent = new Intent(ContactsPanelActivity.this, ChatActivity.class);
                intent.putExtra(EaseConstant.EXTRA_USER_ID, mContacts.getImKey());
                startActivity(intent);
            }
        });
    }

    @Override
    protected void updateViews() {

    }

}
